<?php
/**
 * 首页控制器
 */
class IndexController extends CommonController{
    //加载首页模板
    public function index(){
        //处理导航分类
		//调用导航方法
		$this->navigation();
		//调取首页分类
		$this->cate();
		//处理轮播图
		$roll = K('RollPicture')->all();
		$this->assign('roll',$roll);
		//获取列商品数据
		$cates = K('Category')->all();
		//获取女人广场数据
		$womenCid = K('Category')->getSon($cates,4);
		$womenData = $this->goodsDataObj($womenCid)->order('click DESC')->limit('6')->all();
		$this->assign('womenData',$womenData);
		//获取男人广场
		$manCid = K('Category')->getSon($cates,5);
		$manData = $this->goodsDataObj($manCid)->order('click DESC')->limit('4')->all();
		$this->assign('manData',$manData);
		//获取生活广场
		$lifeCid = K('Category')->getSon($cates,6);
		$lifeData = $this->goodsDataObj($lifeCid)->order('click DESC')->limit('8')->all();
		$this->assign('lifeData',$lifeData);
		//获取手工作坊
		$handCid = K('Category')->getSon($cates,8);
		$handData = $this->goodsDataObj($handCid)->order('click DESC')->limit('8')->all();
		$this->assign('handData',$handData);
		//获取vintage
		$vinCid = K('Category')->getSon($cates,9);
		$vinData = $this->goodsDataObj($vinCid)->order('click DESC')->limit('4')->all();
		$this->assign('vinData',$vinData);
		//处理个人主页区域块的数据
		if(isset($_SESSION['uid'])){
			$orders2 = K('Orders')->where("state=2 AND user_uid={$_SESSION['uid']}")->count();
			$orders0 = K('Orders')->where("state=0 AND user_uid={$_SESSION['uid']}")->count();
			$this->assign('orders2',$orders2);
			$this->assign('orders0',$orders0);
		}
		//处理诞生精品数：喜欢数大于10的为精品
		$count = K('Goods')->where("likes>=20")->count();
		$this->assign('count',$count);
		//限时购:输出点击次数大于200的商品
		$hotData = M('')->join('__goods__ as g JOIN __goods_detail__ as gd ON g.gid = gd.goods_gid')->field("bigpic,smallpic,gid,click,category_cid")->where("click>=200")->order("rand()")->limit('2')->all();
		$this->assign('hotData',$hotData);
        $this->display();
    }
	
	/**
	 * 关联三表
	 * 为获取各个楼层的数据做准备
	 */
	private function goodsDataObj($cid){
		return M('')->join('__goods__ as g JOIN __goods_detail__ as gd ON g.gid = gd.goods_gid JOIN category as c ON g.category_cid = c.cid')->field("cname,bigpic,smallpic,gid,click,category_cid")->where("category_cid IN(" . implode(',', $cid) . ")");
	}
	
	
	
	
}
